package com.migu.spms.utils;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 
* 项目名称:  SPMS权限申请
* 包:        com.migu.spms.utils 
* 类名称:    HttpClient.java
* 类描述:    http请求工具类
* 创建人:    fw  
* 创建时间:  2017年4月7日 下午2:17:51
 */
public class HttpClient {
    private static String prefix = "http://";
    private static String suffix = "?sessionId=";
    private static String contentType = "text/json;charset=UTF-8";
    private static String charSet = "UTF-8";

    public static String postRequest(String url, String path, String json, int timeout) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        StringBuffer sb = new StringBuffer(prefix);
        sb.append(url).append(path);
        HttpPost httpPost = new HttpPost(sb.toString());
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(2000).build();
        httpPost.setConfig(requestConfig);
        try {
            StringEntity se = new StringEntity(json, charSet);
            se.setContentType(contentType);
            se.setContentEncoding(charSet);
            httpPost.setEntity(se);
            CloseableHttpResponse response = httpclient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                String jsonStr = EntityUtils.toString(entity, charSet);
                EntityUtils.consume(entity);
                return jsonStr;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String getRequest(String url, String path, int timeout) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        StringBuffer s = new StringBuffer(prefix);
        s.append(url).append(path);
        HttpGet httpGet = new HttpGet(s.toString());
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(2000).build();
        httpGet.setConfig(requestConfig);
        try {
            CloseableHttpResponse response = httpclient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                String jsonStr = EntityUtils.toString(entity, charSet);
                EntityUtils.consume(entity);
                return jsonStr;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    
    public static String postRequest(String url, String path, String json,String sessionId, int timeout) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        StringBuffer sb = new StringBuffer(prefix);
        sb.append(url).append(path);
        HttpPost httpPost = new HttpPost(sb.toString());
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(2000).build();
        httpPost.setConfig(requestConfig);
        try {
            StringEntity se = new StringEntity(json, charSet);
            se.setContentType(contentType);
            se.setContentEncoding(charSet);
            httpPost.setEntity(se);
            System.out.println("request:"+url+"\n"+json);
            CloseableHttpResponse response = httpclient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                String jsonStr = EntityUtils.toString(entity, charSet);
                EntityUtils.consume(entity);
                return jsonStr;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
